package io.renren.modules.canteens.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.Result;
import io.renren.modules.canteens.dao.SmallCanteenPurchaseDao;
import io.renren.modules.canteens.dto.SmallCanteenPurchaseDTO;
import io.renren.modules.canteens.entity.SmallCanteenPurchaseEntity;
import io.renren.modules.canteens.service.SmallCanteenPurchaseService;
import io.renren.modules.sys.dto.SysUserDTO;
import io.renren.modules.sys.service.SysDeptService;
import io.renren.modules.sys.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 小食堂采购表
 *
 * @author 翟文闯 619556390@QQ.com
 * @since 1.0.0 2019-11-22
 */
@Service
public class SmallCanteenPurchaseServiceImpl extends CrudServiceImpl<SmallCanteenPurchaseDao, SmallCanteenPurchaseEntity, SmallCanteenPurchaseDTO> implements SmallCanteenPurchaseService {

    @Autowired
    private SmallCanteenPurchaseDao smallCanteenPurchaseDao;
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private SysUserService sysUserService;
    @Override
    public QueryWrapper<SmallCanteenPurchaseEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<SmallCanteenPurchaseEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public Map getOrderList(String createDate, long canteenId, int limit, int page) {
            Map  map =new HashMap<>();
            if(StringUtils.isBlank(createDate)){
                Date date = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                createDate = sdf.format(date);
            }
            String  paging;
            if(0 ==limit){
                paging="";
            }else {
                paging="limit " + (page - 1) * limit + "," + limit;
            }
            createDate = " and  DATE_FORMAT(A.create_date,'%Y-%m')=DATE_FORMAT('"+ createDate +"','%Y-%m') ";
            List<SmallCanteenPurchaseDTO> orderList = smallCanteenPurchaseDao.getOrderList(createDate,canteenId,paging);
            SmallCanteenPurchaseDTO order = smallCanteenPurchaseDao.getOrdertotalMony(createDate, canteenId);
            SmallCanteenPurchaseDTO totalPage = smallCanteenPurchaseDao.getTotalPage(createDate, canteenId);
            if(orderList.size()<1 || null ==order){
                Result result = new Result();
                if(null==createDate){
                    result.setMsg("当前月没有数据");
                }
                result.setCode(5001);
                result.setMsg(createDate+"没有数据");
                map.put("result",result);
                return map;
            }
            String name = sysDeptService.get(canteenId).getName();
            SysUserDTO sysUserDTO;
            for (SmallCanteenPurchaseDTO orderDTO:orderList) {
                if("新食堂".equals(name)){
                    orderDTO.setCanteenName("小食堂"+"每日进货清单");
                }else {
                    orderDTO.setCanteenName("大食堂"+"每日进货清单");
                }
                sysUserDTO = sysUserService.get(orderDTO.getChedId());
                if(null !=sysUserDTO){
                    orderDTO.setChedName(sysUserDTO.getRealName());
                }
                sysUserDTO = sysUserService.get(orderDTO.getCanteenAdminId());
                if(null !=sysUserDTO){
                    orderDTO.setCanteenAdminName(sysUserDTO.getRealName());
                }
            }
            map.put("totalPage",totalPage.getTotalPage());
            map.put("orderList",orderList);
            map.put("totalPrice",order.getTotalPrice());
            return map;
        }



}